﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;

namespace XPathAutoSuggest {
    /// <summary>
    /// Base class for commands that makes some repeated code available to all inheritors
    /// </summary>
    /// <remarks>
    /// <para>
    /// Every ICommand interface has a View model IXPathToolVm that is uses (inversion of control)
    /// to actually execute commands. This base class simplifies that design principle. Additionally,
    /// it implements the CanExecuteChange method for all inheritors, since this method is
    /// required to implement ICommand, yet it never varies. By using a base class, we can
    /// reduce the busy work.
    /// </para>
    /// <para>
    /// Also note that every command must implement ICommand, here the base class does that on behalf
    /// of all its children. ICommand requires the methods Execute, CanExecute and CanExecuteChanged.
    /// </para>
    /// </remarks>
    public class CommandBase : ICommand {
        /// <summary>The ModelView which implements our command methods</summary>
        protected IXPathToolVm _MV;

        /// <summary>
        /// Constructor takes a view model to control and holds it
        /// </summary>
        /// <param name="newModelView">The view model that utilizes the command</param>
        public CommandBase(IXPathToolVm newModelView) {
            _MV = newModelView;
        }

        #region ICommand Members

        /// <summary>
        /// Returns true when the command can fire
        /// </summary>
        /// <param name="parameter">Optional parameter</param>
        /// <returns>True if the command can fire, otherwise false</returns>
        /// <remarks>
        /// The method is virtual because inherting classes will almost always provide their own
        /// implementation
        /// </remarks>
        public virtual bool CanExecute(object parameter) {
            return false;
        }

        /// <summary>
        /// Hooks-in to the command manager to drive the CanExecute
        /// </summary>
        public event EventHandler CanExecuteChanged {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        /// <summary>
        /// Does the actual work
        /// </summary>
        /// <param name="parameter">Optional parameter</param>
        /// <remarks>Virtual method that inheriting classes need to handle themselves</remarks>
        public virtual void Execute(object parameter) {
        }

        #endregion
    }
}
